//*
// * Definition for a binary tree node.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    void _tree2str(TreeNode* root, string& s)
    {
        if (root == nullptr)
            return;
        s += to_string(root->val);
        if (root->left || root->right)
        {
            s += '(';
            _tree2str(root->left, s);
            s += ')';
        }
        if (root->right)
        {
            s += '(';
            _tree2str(root->right, s);
            s += ')';
        }
    }
    string tree2str(TreeNode* root) {
        string res;
        if (root == nullptr) return res;

        _tree2str(root, res);
        return res;
    }
};

